package com.haha.mining;

import javax.sound.sampled.*;
import java.io.File;

/**
 * @author shenlinnan
 * @date 2025/8/10 11:46
 */
public class AudioPlayer {

    public static void playAudio(String filePath) {
        try {
            // 1. 获取音频输入流
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(new File(filePath));

            // 2. 获取音频格式
            AudioFormat format = audioStream.getFormat();

            // 3. 创建Clip对象
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip audioClip = (Clip) AudioSystem.getLine(info);

            // 4. 加载音频数据并播放
            audioClip.open(audioStream);
            audioClip.start();

            // 5. 等待播放完成
            System.out.println("播放中...");
            while (audioClip.isRunning()) {
                Thread.sleep(100);
            }

            // 6. 关闭资源
            audioClip.close();
            audioStream.close();
            System.out.println("播放完成");

        } catch (UnsupportedAudioFileException e) {
            System.err.println("不支持的音频格式: " + e.getMessage());
        } catch (LineUnavailableException e) {
            System.err.println("音频线路不可用: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("播放错误: " + e.getMessage());
        }
    }

    public static void playAudio(){
        playAudio("J:\\World of Warcraft\\_classic_\\Interface\\AddOns\\BiaoGe\\Media\\sound\\AI\\老公加油.mp3");
    }

}
